<template>
  <div>
    <c-table-box
      ref="cTableBox"
      hide-func
      checkbox
      :other-filters="otherFilters"
      :table-config="tableConfig"
      @selection-change="selectionChange"
    >
      <template #btn>
        <c-permission v-if="type==='all'" code="all" />
        <c-permission v-if="type==='submit'" code="submit">
          <c-button type="primary" opt-code="termination" @click="handleTerminal">终止</c-button>
          <c-button type="primary" opt-code="edit" @click="handleChange">编辑</c-button>
        </c-permission>
        <c-permission v-if="type==='audit'" code="audit">
          <c-button type="primary" opt-code="audit" @click="handleAudit">审核</c-button>
        </c-permission>
      </template>

    </c-table-box>
  </div>
</template>
<script>
import { otherFiltersItem } from '@/utils/index'
import { terminal } from '@/api/shopChangeRule'

export default {
  name: 'List',

  props: {
    type: {
      type: String,
      default: ''
    },
    activeType: {
      type: String,
      default: ''
    },
    serachFilters: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      formVisible: false,
      activeTabIndex: 0,
      actvieApplyCode: '',
      typeFilters: [],
      handlefilters: [],
      formData: { auditResult: '', auditSuggest: '' }, // 审核表单
      auditVisible: false, // 审核弹窗
      tableSelected: [],
      activeRowData: {}, // 当前选中行
      activeRowId: '',
      activeName: '',
      showAudit: false,
      showDetail: false,
      showChange: false,
      tableConfig: {
        interfacePath: `${process.env.VUE_APP_BASE_API}/api/v1/shopRebateChange/page`,
        buttonGroupCode: 'default',
        hideSearchBar: true,
        tabIndex: 0,
        pageSize: 10,
        order: 'desc',
        sort: 'createTime',
        // 表格字段动态配置
        setting: [
          {
            prop: 'changeCode',
            label: '返利变更单号',
            minWidth: '130',
            className: 'btn-green',
            isButton: true,
            clickFun: this.handleDetail
          },
          {
            prop: 'applyCode',
            label: '返利申请单号',
            minWidth: '130'
          },
          {
            prop: 'state',
            label: '返利变更进度',
            propEnum: {
              domainCode: 'marketing',
              appCode: 'slm',
              dictTypeCode: 'SHOP_REBATE_CHANGE_STATE'
            },
            minWidth: '125'
          },
          {
            prop: 'constCode',
            label: '门店建设项目号',
            minWidth: '130'
          },

          {
            prop: 'marketType',
            label: '报图类型',
            minWidth: '130',
            propEnum: {
              domainCode: 'marketing',
              appCode: 'slm',
              dictTypeCode: 'MARKET_TYPE'
            }
          }, {
            prop: 'constType',
            label: '门店建设类型',
            minWidth: '130',
            propEnum: {
              domainCode: 'marketing',
              appCode: 'slm',
              dictTypeCode: 'CONST_TYPE'
            }
          }, {
            prop: 'schedule',
            label: '门店开业进度',
            minWidth: '130',
            propEnum: {
              domainCode: 'marketing',
              appCode: 'slm',
              dictTypeCode: 'OPEN_SCHEDULE'
            }
          }, {
            prop: 'isMonthReturn',
            label: '是否涉及月返',
            minWidth: '130',
            propEnum: {
              domainCode: 'marketing',
              appCode: 'slm',
              dictTypeCode: 'BOOLEANS'
            }
          },

          {
            prop: 'acceptWholeTotalArea',
            label: '验收整装总面积',
            minWidth: '130'
          },
          {
            prop: 'acceptSimpleTotalArea',
            label: '验收简装总面积',
            minWidth: '130'
          }
        ]
      }
    }
  },
  computed: {
    otherFilters() {
      return [...this.serachFilters, ...this.typeFilters]
    },
    selectedIds() {
      return this.tableSelected.map(row => {
        return row.id
      })
    }
  },
  watch: {
    type: {
      handler(v) {
        this.setOtherFilters(v)
      },
      immediate: true
    },
    serachFilters: {
      handler(v) {
        if (this.activeType !== this.type) return
        this.$nextTick(() => {
          this.$refs.cTableBox.initTableData()
        })
      }
    }
  },
  activated() {
    if (this.ready && this.refreshTable) {
      this.refreshTable()
    }
  },
  deactivated() {
    this.ready = true
  },
  created() {
  },
  methods: {
    handleTerminal() {
      if (!this.singleJudge(this.tableSelected, '终止', false)) {
        return
      }
      this.$confirm(`是否确认将勾选的【${this.tableSelected.length}条】数据终止?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 调用接口执行终止
        terminal(this.selectedIds).then(res => {
          if (res.success === 1) {
            this.$message.success('终止成功')
            this.refreshTable(true)
          }
        })
      }).catch(() => {})
    },
    // 审核
    handleAudit() {
      if (!this.singleJudge(this.tableSelected, '审核', false)) {
        return
      }
      this.$router.push({ name: 'RebateChangeAudit', query: { id: this.tableSelected[0].id, applyCode: this.tableSelected[0].applyCode }})
    },
    handleDetail(data) {
      this.$router.push({ name: 'RebateChangeDetail', query: { id: data.id, applyCode: data.applyCode }})
    },
    handleChange() {
      if (!this.singleJudge(this.tableSelected, '编辑', true)) {
        return
      }
      this.$router.push({ name: 'RuleChange', query: { id: this.tableSelected[0].id, applyCode: this.tableSelected[0].applyCode }})
    },
    selectionChange(v) {
      this.tableSelected = v
    },

    // 设置过滤条件
    setOtherFilters(v) {
      switch (v) {
        case 'all':
          this.typeFilters = []
          break
        case 'submit':
          // 显示已保存A
          this.typeFilters = [otherFiltersItem('state', 'A', '='),
            otherFiltersItem('isEnd', '0', '=')
          ]
          break
        case 'audit':
          // 显示待审核B
          this.typeFilters = [otherFiltersItem('state', 'B', '='),
            otherFiltersItem('isEnd', '0', '=')]
          break
      }
    },

    // 刷新表格
    refreshTable(v = 'onPage') {
      this.$nextTick(() => {
        this.$refs.cTableBox.initTableData(v)
      })
    }
  }
}
</script>
